package com.集合.ArrayList;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo {
    public static void main(String[] args) {
        //创建list集合对象
        List<Student> l = new ArrayList<Student>();
        //创建学生对象
        Student s1 = new Student("张三",20);
        Student s2 = new Student("李四",30);
        //添加学生对象到List集合
        l.add(s1);
        l.add(s2);

        //遍历集合
       /* //方法一:迭代器遍历
        Iterator<Student> it = l.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        System.out.println("------------------");
        //方法二:for循环遍历
        for(int i=0;i<l.size();i++){
            System.out.println(l.get(i));
        }*/



        //不重写toString()方法
        //方法一:迭代器遍历
        Iterator<Student> it = l.iterator();
        while(it.hasNext()){
            Student s = it.next();
            System.out.println(s.getName()+", "+s.getAge());
        }
        System.out.println("------------------");
        //方法二:for循环遍历
        for(int i=0;i<l.size();i++){
            Student ss = l.get(i);
            System.out.println(ss.getName()+", "+ss.getAge());
        }
    }
}
